<template>
  <div>
    <div class="content-header">
      <h1 style="margin:0 0 0 0">检查项管理</h1>
      <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb" style="font-size: 5px">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>体检管理</el-breadcrumb-item>
        <el-breadcrumb-item>检查项管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="content-main">
      <div style="padding:10px 0 5px 0 ;line-height: 34px;float: left">
        <el-input v-model="pageNation.queryString" placeholder="项目编码/项目名称" style="width: 200px;padding-right: 5px" size="small"></el-input>
        <el-button type="primary" @click="findByCondition" size="small" icon="el-icon-search">查询</el-button>
        <el-button type="primary" @click="shouAddView" size="small" icon="el-icon-plus">新增</el-button>
      </div>
      <div>
        <el-table :data="checkItemList" border style="width: 100%" height="600px" >
          <el-table-column type="index" align="center" label="序号" width="50"></el-table-column>
          <el-table-column prop="code" label="项目编码" align="center" width="80"></el-table-column>
          <el-table-column prop="name" label="名称" align="center" width="200"></el-table-column>
          <el-table-column prop="sex" label="适用性别" align="center" width="80">
            <template slot-scope="scope">
              <span>{{scope.row.sex == '0' ? '不限' : scope.row.sex == '1' ? '男' : '女'}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="age" label="适用年龄" align="center" width="100"></el-table-column>
          <el-table-column prop="price" label="价格" align="center" width="80"></el-table-column>
          <el-table-column prop="remark" label="项目说明" align="center" width="200"></el-table-column>
          <el-table-column prop="attention" label="注意事项" align="center" ></el-table-column>
          <el-table-column label="操作" align="center" width="200">
            <template slot-scope="scope">
              <el-button type="primary" size="small" @click="showUpdateItem(scope.row)">编辑</el-button>
              <el-button type="danger" size="small" @click="deleteItem(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="block">
        <el-pagination
            background
            @current-change="handleCurrentPage"
            @size-change="handleSizeChange"
            :current-page="pageNation.currentPage"
            :page-size="pageNation.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :page-sizes="[5, 10, 20, 30]"
            :total="pageNation.total">
        </el-pagination>
      </div>
      <!--新增检查项对话框-->
      <el-dialog title="新增检查项对话框" :visible.sync="dialogFormVisible">
        <el-form ref="dataAddForm" :rules="rules" :model="addItem" label-position="right" label-width="100px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="项目编码" prop="code">
                <el-input v-model="addItem.code" style="width: 220px"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="项目名称" prop="name">
                <el-input v-model="addItem.name" ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="性别">
                <el-select v-model="addItem.sex">
                  <el-option label="不限" value="0"></el-option>
                  <el-option label="男" value="1"></el-option>
                  <el-option label="女" value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="适用年龄">
                <el-input v-model="addItem.age"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="类型">
                <el-select v-model="addItem.type">
                  <el-option label="检查" value="1"></el-option>
                  <el-option label="检验" value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="价格">
                <el-input v-model="addItem.price"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="项目说明">
                <el-input v-model="addItem.remark" type="textarea"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="注意事项">
                <el-input v-model="addItem.attention" type="textarea"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleAddItem">确 定</el-button>
        </div>
      </el-dialog>
      <!--更新检查项对话框-->
      <el-dialog title="更新检查项对话框" :visible.sync="dialogFormVisible4Edit">
        <el-form ref="dataUpdateForm" :rules="rules" :model="addItem" label-position="right" label-width="100px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="项目编码" prop="code">
                <el-input v-model="addItem.code" style="width: 220px"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="项目名称" prop="name">
                <el-input v-model="addItem.name" ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="性别">
                <el-select v-model="addItem.sex">
                  <el-option label="不限" value="0"></el-option>
                  <el-option label="男" value="1"></el-option>
                  <el-option label="女" value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="适用年龄">
                <el-input v-model="addItem.age"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="类型">
                <el-select v-model="addItem.type">
                  <el-option label="检查" value="1"></el-option>
                  <el-option label="检验" value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="价格">
                <el-input v-model="addItem.price"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="项目说明">
                <el-input v-model="addItem.remark" type="textarea"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="注意事项">
                <el-input v-model="addItem.attention" type="textarea"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateItem">确 定</el-button>
        </div>
      </el-dialog>
    </div>

  </div>
</template>

<script>
import {getRequest} from "../../utils/api";
import {postRequest} from "../../utils/api";
import {deleteRequest} from "../../utils/api";
import {putRequest} from "../../utils/api";

export default {
  name: "checkItem",
  data(){
    return{
      pageNation:{
        currentPage: 1,
        pageSize: 10,
        total: 0,
        queryString: null
      },
      checkItemList:[],
      dialogFormVisible:false,
      dialogFormVisible4Edit:false,
      addItem: {
        code : '',
        name: '',
        sex: '',
        age: '0-150',
        price: '',
        type: '',
        remark: '',
        attention: '暂时无！'
      },
      rules: {//校验规则
        code: [{ required: true, message: '项目编码为必填项', trigger: 'blur' }],
        name: [{ required: true, message: '项目名称为必填项', trigger: 'blur' }]
      }
    }
  },
  methods: {
    handleAddItem(){
      // console.log(this.addItem);
      this.$refs['dataAddForm'].validate((valid) =>{
        if(valid){
          postRequest("/checkItem/",this.addItem).then(res=>{
            if(res){
              this.dialogFormVisible = false;
              this.addItem = {};
              this.findByCondition();
            }
          })
        }else{
          this.$message.error("校验不通过，请重新输入！");
        }
      });
    },
    deleteItem(id){
      // console.log(id);
      //弹出确认框
      this.$confirm('你确定要删除当前数据吗？','提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //发送ajax请求，将id提交到Controller
        deleteRequest("/checkItem/" +id).then((res) => {
          if(res.status ==200){
            this.$message({
              type:'success',
              message:res.message
            });
            this.findByCondition();
          }else{
            //执行失败
            this.$message.error(res.message);
          }
        }).catch((r) => {
          this.showMessage(r);
        });
      }).catch(() => {
        this.$message("已取消");
      });
    },
    updateItem(){
      // console.log(this.addItem);
      this.$refs['dataUpdateForm'].validate((valid)=>{
        if(valid){
          putRequest("/checkItem/",this.addItem).then(res=>{
            if(res){
              this.dialogFormVisible4Edit = false;
              this.findByCondition();
            }
          })
        }else{
          this.$message.error("校验不通过，请重新输入！");
        }
      })
    },
    showUpdateItem(item){
      this.dialogFormVisible4Edit = true;
      this.addItem = item;
    },
    findByCondition() {
      getRequest("/checkItem/page",this.pageNation).then(res=>{
        if(res){
          this.pageNation.total = res.total;
          this.checkItemList = res.data;
        }
      })
    },
    shouAddView(){
      this.dialogFormVisible = true;
    },
    handleSizeChange(pageSize){
      this.pageNation.pageSize = pageSize;
      this.findByCondition();
    },
    handleCurrentPage(currentPage){
      this.pageNation.currentPage = currentPage;
      this.findByCondition();
    },
  },
  created() {
    this.findByCondition();
  },

}
</script>

<style scoped>

</style>